﻿using System;
using System.Data;
using System.Web.UI.WebControls;

public partial class PlatiSpreAchitare : System.Web.UI.Page
{
    private readonly string mCurrentModule = Credits.Module.ID;
    private readonly string mPageName = "Crearea si administrarea Platilor";
    private string appPath = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        appPath = Utils.GetApplicationPath(Request);
        Utils.GetMaster(this).PerformPreloadActions(mCurrentModule, mPageName);

        bool allowHere = Utils.PermissionAllowed(mCurrentModule, Credits.Domains.ManageLoans.Name, Constants.Constants.Classifiers.Permissions_View);
        if (allowHere)
        {
            if (!IsPostBack)
            {
                ShowPanel(loanSelectionDialog.ID);
            }
        }
    }

    private void ShowPanel(string panelName)
    {
        #region Hide panels

        previewPanel.Visible = false;
        emptyPanel.Visible = false;
        loanSelectionDialog.Hide();

        #endregion Hide panels

        try
        {
            #region Get Panel Name

            switch (panelName)
            {
                case "loanSelectionDialog":
                    loanSelectionDialog.BindDataSource();
                    loanSelectionDialog.Show();
                    break;

                case "previewPanel":
                    previewPanel.Visible = true;
                    break;
             
                case "emptyPanel":
                    emptyPanel.Visible = true;
                    break;

                default:
                    emptyPanel.Visible = true;
                    break;
            }

            #endregion Get Panel Name
        }
        catch (Exception ex)
        {
            Utils.InfoText(this, "Atentie! Eroare in sistem!", ex.Message);
        }
    }

    protected void loanSelectionDialog_Selected(object sender, LoanSelectionDialog.FilterWindowEventsArg e)
    {
        FillPreviewForm(e.SelectedItem);
        ShowPanel(previewPanel.ID);
    }

    protected void ClearPreviewForm()
    {
        dateNowLabel.Text = string.Empty; 
        clientNameLabel.Text = string.Empty;
        loanNrLabel.Text = string.Empty;
        loanInitialChangeRateLabel.Text = string.Empty;
        loanRealChangeRate.Text = string.Empty;

        previewLoanGridView.DataSource = null;
        previewLoanGridView.DataBind();
    }

    protected void FillPreviewForm(int loanID)
    {
        try
        {        
            ClearPreviewForm();
            dateNowLabel.Text = DateTime.Now.ToString(Constants.Constants.ISODateBackwardDotsFormat);

            if (loanID != 0)
            {
                Credits.DataObjects.LoanObject loanInfo = Utils.ModuleCredits().GetLoanObjectByID(loanID);
                if (loanInfo != null)
                {
                    clientNameLabel.Text = loanInfo.ClientFirstLastName;
                    loanNrLabel.Text = loanInfo.ContractCode.ToString();
                 
                    if (loanInfo.CurrencyID != 0 && loanInfo.CurrencyID != (int)Constants.Constants.CurrencyList.MDL)
                    {
                        decimal initialChangeRate = loanInfo.CurrencyChangeRate;
                        decimal realChangeRate = Utils.ModuleCredits().GetCurrencyRateByDate(DateTime.Now.Date, loanInfo.CurrencyID);

                        loanInitialChangeRateLabel.Text = loanInfo.CurrencyChangeRate.ToString();
                        loanRealChangeRate.Text = realChangeRate.ToString();

                        if (initialChangeRate < realChangeRate)
                        {
                            Utils.ModuleCredits().ApplyNewChangeRateForloanOrders(loanID, realChangeRate, DateTime.Now.Date);                               
                        }
                    }                    

                    DataTable loanOrders = Utils.ModuleCredits().GetLoanOrderList(loanID);
                    loanOrders.Columns.Add("comisionAdm", typeof(decimal));

                    previewLoanGridView.DataSource = loanOrders;
                    previewLoanGridView.DataBind();
                }
            }
        }
        catch (Exception ex)
        {
            Utils.InfoText(this, "Atentie! Eroare in sistem!", ex.Message);
        }
    }


    decimal sumRateAmountForFooter = 0;
    decimal sumPercentAmountForFooter = 0;
    decimal sumPenaltyForFooter = 0;
    decimal sumCorrentionAmountForFooter = 0;
    decimal sumComisionForFooter = 0;
    decimal sumTotalAmountForFooter = 0;
    DateTime curretDate = DateTime.Now;

    protected void previewLoanGridView_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
    {
        try
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label dataLabel = (Label)e.Row.FindControl("dataLabel");
                if (dataLabel != null)
                {
                    DateTime tempVal = Crypt.Utils.ToDateTime(dataLabel.Text, Constants.Constants.ISODateBackwardDotsFormat);

                    if (tempVal < curretDate)
                    {
                        e.Row.BackColor = System.Drawing.Color.Yellow;
                    }
                }

                Label rateAmountLabel = (Label)e.Row.FindControl("rateAmountLabel");
                if (rateAmountLabel != null)
                {
                    decimal tempVal = 0;
                    decimal.TryParse(rateAmountLabel.Text, out tempVal);
                    sumRateAmountForFooter += tempVal;
                }

                Label percentAmountLabel = (Label)e.Row.FindControl("percentAmountLabel");
                if (percentAmountLabel != null)
                {
                    decimal tempVal = 0;
                    decimal.TryParse(percentAmountLabel.Text, out tempVal);
                    sumPercentAmountForFooter += tempVal;
                }

                Label penaltyLabel = (Label)e.Row.FindControl("penaltyLabel");
                if (penaltyLabel != null)
                {
                    decimal tempVal = 0;
                    decimal.TryParse(penaltyLabel.Text, out tempVal);
                    sumPenaltyForFooter += tempVal;
                }


                Label rateCorrectionLabel = (Label)e.Row.FindControl("rateCorrectionLabel");
                if (rateCorrectionLabel != null)
                {
                    decimal tempVal = 0;
                    decimal.TryParse(rateCorrectionLabel.Text, out tempVal);
                    sumCorrentionAmountForFooter += tempVal;
                }

                Label comisionAdmLabel = (Label)e.Row.FindControl("comisionAdmLabel");
                if (comisionAdmLabel != null)
                {
                    decimal tempVal = 0;
                    decimal.TryParse(comisionAdmLabel.Text, out tempVal);
                    sumComisionForFooter += tempVal;
                }

                Label totalAmountLabel = (Label)e.Row.FindControl("totalAmountLabel");
                if (totalAmountLabel != null)
                {
                    decimal tempVal = 0;
                    decimal.TryParse(totalAmountLabel.Text, out tempVal);
                    sumTotalAmountForFooter += tempVal;
                }
            }

            if (e.Row.RowType == DataControlRowType.Footer)
            {
                Label totalRateAmountLabel = (Label)e.Row.FindControl("totalRateAmountLabel");
                totalRateAmountLabel.Text = sumRateAmountForFooter.ToString();

                Label totalPercentAmountLabel = (Label)e.Row.FindControl("totalPercentAmountLabel");
                totalPercentAmountLabel.Text = sumPercentAmountForFooter.ToString();

                Label totalPenaltyLabel = (Label)e.Row.FindControl("totalPenaltyLabel");
                totalPenaltyLabel.Text = sumPenaltyForFooter.ToString();

                Label totalrateCorrectionLabel = (Label)e.Row.FindControl("totalrateCorrectionLabel");
                totalrateCorrectionLabel.Text = sumCorrentionAmountForFooter.ToString();

                Label totalComisionAdmLabel = (Label)e.Row.FindControl("totalComisionAdmLabel");
                totalComisionAdmLabel.Text = sumComisionForFooter.ToString();

                Label totalTotalAmountLabel = (Label)e.Row.FindControl("totalTotalAmountLabel");
                totalTotalAmountLabel.Text = sumTotalAmountForFooter.ToString();
            }
        }
        catch (Exception ex)
        {
            Utils.InfoText(this, "Atentie! Eroare in sistem!", ex.Message);
        }
    }

}